package com.bs.blogspace.service.impl;

import com.bs.blogspace.mapper.UserMapper;
import com.bs.blogspace.model.Result;
import com.bs.blogspace.pojo.User;
import com.bs.blogspace.service.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Result<User> register(String userName,String password,String phone) {
        Result<User> result = new Result<>();
        /*判断用户名是否被注册*/
        User byUserName = userMapper.findByUserName(userName);
        if (byUserName!=null){
            result.setMessage("此用户名已被注册");
            result.setCode("604");
            return result;
        }
        if (StringUtils.isEmpty(userName)){
            result.setMessage("用户名不能为空");
            result.setCode("600");
            return result;
        }
        if (StringUtils.isEmpty(password)){
            result.setMessage("密码不能为空");
            result.setCode("601");
            return result;
        }
        User userPhone = userMapper.findByPhone(phone);
        if (userPhone != null){
            result.setMessage("该手机号已经被注册");
            result.setCode("604");
            return result;
        }
        // 密码加自定义盐值，确保密码安全
        String saltPwd = password + "_ykd2050";
        // 生成md5值，并转为大写字母
        String md5Pwd = DigestUtils.md5Hex(saltPwd).toUpperCase();

        User user = new User();
        user.setUserName(userName);
        user.setPassword(md5Pwd);
        user.setPhone(phone);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userMapper.add(user);
        result.setSuccess(true);
        result.setCode("200");
        result.setMessage("注册成功");
        result.setData(user);
        return result;
    }

    @Override
    public Result<User> login(String phone, String password) {
        Result<User> result = new Result<>();
        if (StringUtils.isEmpty(phone)){
            result.setMessage("手机号不能为空");
            result.setCode("600");
        }
        if (StringUtils.isEmpty(password)){
            result.setMessage("密码不能为空");
            result.setCode("601");
            return result;
        }
        User userPhone = userMapper.findByPhone(phone);
        if (userPhone == null){
            result.setCode("500");
            result.setMessage("手机号未注册");
            return result;
        }
        if (StringUtils.isEmpty(userPhone.getPhone())){
            result.setMessage("该手机号没有注册");
            result.setCode("602");
            return result;
        }
        String saltPwd = password + "_ykd2050";
        // 生成md5值，并转为大写字母
        String md5Pwd = DigestUtils.md5Hex(saltPwd).toUpperCase();
        if (!md5Pwd.equals(userPhone.getPassword())){
           result.setMessage("密码错误");
           result.setCode("603");
           return result;
        }
        result.setSuccess(true);
        result.setMessage("登录成功");
        result.setCode("200");
        User user = new User();
        user.setPhone(phone);
        user.setPassword(password);
        result.setData(userPhone);
        return result;
    }

    @Override
    public User getByPhone(String phone) {
        User user = new User();
        if (StringUtils.isEmpty(phone)){
            return user;
        }
        user = userMapper.findByPhone(phone);
        if (user == null){
            return user;
        }
        return user;
    }

    @Override
    public Result<User> update(User user) {
        Result<User> result = new Result<>();
        user.setUpdateTime(LocalDateTime.now());
        result.setSuccess(true);
        result.setMessage("更新成功");
        result.setCode("200");
        result.setData(user);
        userMapper.update(user);
        return result;
    }

    @Override
    public Result<User> queryGetByPhone(String phone) {
        Result<User> result = new Result<>();
        result.setSuccess(true);
        result.setMessage("查询成功");
        result.setCode("200");
        User users = userMapper.queryGetByPhone(phone);
        result.setData(users);
        return result;
    }
}
